import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.StringTokenizer;
import cern.colt.map.OpenIntIntHashMap;

public class JoostTest extends Setup{
	static String FILE_PATH_GENRE="../track1/genreData1.txt";
	static String FILE_PATH_ARTIST="../track1/artistData1.txt";
	static String FILE_PATH_ALBUM="../track1/albumData1.txt";
	static String FILE_PATH_TRACK="../track1/trackData1.txt";
	static String FILE_PATH = "../track1/trainIdx1.txt.item.tax";
	
	public static void main(String args[]){
		USERA = (User[]) readSerialized("../track1/trainIdx1.txt.ser.normal");
		int count = 0;
		for(User user : USERA){
			if(user == null){
				System.out.println("User: " + count + " was removed");
			}
			count ++;
		}
		
	}
	
	private static void testTaxonomy(){
		Artist artist;
		Album Album;
		Track track;
		
		try {

			for(int i=0; i<100;i++){
				System.out.println(ITEMA[i].getClass());
				if(ITEMA[i].getClass() == Artist.class){
					artist = ((Artist)ITEMA[i]);
					System.out.println("Items created by this artist:" + artist.ilen);
					for(int j=0; j<artist.ilen; j++){
						System.out.println(artist.itemsBy[j]);
					}
					
				}
				
				if(ITEMA[i].getClass() == Album.class){
					Album = ((Album)ITEMA[i]);
					System.out.println("Items in this album:" + Album.tlen);
					for(int j=0; j<Album.tlen; j++){
						System.out.println(Album.tracks[j]);
					}
				}
				
				if(ITEMA[i].getClass() == Track.class){
					track = ((Track)ITEMA[i]);
					System.out.println(track.album);
					System.out.println(track.artist);
				}
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private static void readGenre() throws Exception {
		File file = new File(FILE_PATH_GENRE);
		BufferedReader bufRdr = new BufferedReader(new FileReader(file));
		String line = null;
		int genreID = -1;		

		while ((line = bufRdr.readLine()) != null) {
			genreID = Integer.valueOf(line);
			ITEMA[genreID] = new Genre(ITEMA[genreID]);
		}
		bufRdr.close();
	}
	
	private static void readArtist() throws Exception {
		File file = new File(FILE_PATH_ARTIST);
		BufferedReader bufRdr = new BufferedReader(new FileReader(file));
		String line = null;
		int artistID = -1;		

		while ((line = bufRdr.readLine()) != null) {
			artistID = Integer.valueOf(line);
			ITEMA[artistID] = new Artist(ITEMA[artistID]);
		}
		bufRdr.close();
	}
	
	private static void readAlbum() throws Exception {
		File file = new File(FILE_PATH_ALBUM);
		BufferedReader bufRdr = new BufferedReader(new FileReader(file));
		String line = null;
		StringTokenizer st;
		int albumID = -1;
		int artistID = -1;
		Album album;
		Artist artist;

		while ((line = bufRdr.readLine()) != null) {
			st = new StringTokenizer(line, "|");
			albumID = Integer.valueOf(st.nextToken());
			artistID = parse(st.nextToken());
			album = new Album(ITEMA[albumID]);
			
			//Add Artist
			if(artistID >= 0){
				artist = (Artist)ITEMA[artistID];
				artist.putItemBy(album); //Add this album to the artist
				album.artist = artist;
			} else {
				album.artist =null;
			}
			ITEMA[albumID] = album;
		}
		bufRdr.close();
	}
	
	private static void readTrack() throws Exception {
		File file = new File(FILE_PATH_TRACK);
		BufferedReader bufRdr = new BufferedReader(new FileReader(file));
		String line = null;
		StringTokenizer st;
		int trackID = -1;
		int albumID = -1;
		int artistID = -1;
		
		Track track;


		while ((line = bufRdr.readLine()) != null) {
			st = new StringTokenizer(line, "|");
			trackID = Integer.valueOf(st.nextToken());
			albumID = parse(st.nextToken());
			artistID = parse(st.nextToken());
			track = new Track(ITEMA[trackID]);
			
			//Add Album
			if(albumID >= 0){
				((Album)ITEMA[albumID]).putTrack(track); //Add this track to the album
				track.album = (Album)ITEMA[albumID];
			}	else {
				track.album =null;
			}
			
			//Add Artist
			if(artistID >= 0){
				((Artist)ITEMA[artistID]).putItemBy(track); //Add this track to the artist
				track.artist = (Artist)ITEMA[artistID];
			} else {
				track.artist =null;
			}
			
			ITEMA[trackID] = track;
		}
		bufRdr.close();
	}
	
	private static int parse(String str){
		if(str.equals("None")){
			return -1;
		} else {
			return Integer.parseInt(str);
		}
	}
	
	private static void writeSerialisedResult() {
		System.out.println("writeSerialisedResult: " + FILE_PATH + ".ser");

		try {
			OutputStream file = new FileOutputStream(FILE_PATH + ".ser");
			OutputStream buffer = new BufferedOutputStream(file);
			ObjectOutput output = new ObjectOutputStream(buffer);
			try {
				output.writeObject(ITEMA);
			} finally {
				output.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	static void readSerializedIAT() {
		System.out.println("readSerializedIA: " + FILE_PATH + ".ser");

		try {
			InputStream file = new FileInputStream(FILE_PATH + ".ser");
			InputStream buffer = new BufferedInputStream(file);
			ObjectInput input = new ObjectInputStream(buffer);
			try {
				ITEMA = (Item[]) input.readObject();
			} finally {
				input.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
